package cn.schoolwow.workflow.module.task.action.flow.complete.nodeType.gateway.common;

import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import cn.schoolwow.workflow.entity.WorkFlowNodeJump;

import java.util.List;

public class SetSuccessorJumpNodeListCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String gatewayNodeTypeName = flowContext.checkData("gatewayNodeTypeName", String.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        WorkFlowNode gatewayNode = flowContext.checkData("gatewayNode", WorkFlowNode.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        List<WorkFlowNodeJump> successorJumpNodeList = transaction.query(WorkFlowNodeJump.class)
                .addQuery("definitionId", workFlowInstance.getDefinitionId())
                .addQuery("currentNodeId", gatewayNode.getId())
                .execute()
                .getList();
        if(null==successorJumpNodeList||successorJumpNodeList.isEmpty()){
            throw new IllegalArgumentException(gatewayNodeTypeName+"["+gatewayNode.getName()+"]的"
                    + "["+(gatewayNode.getGatewayPosition()==0?"开始位置":"结束位置")+"]"
                    + "的后继节点列表为空");
        }
        flowContext.putCurrentFlowData("successorJumpNodeList", successorJumpNodeList);
    }

    @Override
    public String name() {
        return "设置网关后继节点列表公共流程";
    }
}
